package com.bd.learning.datasource;

import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.DataSourceWrapper;
import cn.hutool.db.ds.druid.DruidDSFactory;
import cn.hutool.log.Log;
import cn.hutool.setting.Setting;
import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DBTest {

    public static void main(String[] args) {
        DBTest dbTest = new DBTest();
        dbTest.test1();
    }


    public void test1() {
        Setting setting = new Setting("config.ini");
        String jdbc = setting.get("umcdb", "url");
        String driver = setting.get("umcdb", "driver");
        String user = setting.get("umcdb", "user");
        String pass = setting.get("umcdb", "pass");

        DataSourceWrapper dataSourceWrapper = (DataSourceWrapper) DruidDSFactory.setCurrentDSFactory(new DruidDSFactory(setting)).getDataSource("umcdb");
        DruidDataSource druidDataSource = (DruidDataSource) dataSourceWrapper.getRaw();
        DataSource ds = dataSourceWrapper.getRaw();
        druidDataSource.setInitialSize(2);
        druidDataSource.setMinIdle(1);
        druidDataSource.setMaxActive(10);
        Connection connection = null;
        try {
            connection = druidDataSource.getConnection();
            Log.get().info("connection={}", connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}
